#!/bin/bash
# March 26, 2006 - Dan Van Wormer - Puppy Linux

# Determine the path to this application.
APPDIR=`dirname "$0"`
cd "$APPDIR"
APPDIR="`pwd`"

# Find the name of the file.
function notAForwardSlash
{
fSlash=/
   if [[ $1 != $fSlash ]]

        then
      return 0
        else
           return 1
        fi
}

function getFileName
{
   STRING=$1

        LENGTH=${#STRING}

   for ((n=0;n <= $LENGTH; n++))
        do
      CHAR=${STRING:$n:1}

      if notAForwardSlash $CHAR
      then
         FileName=$FileName$CHAR

      else
         FileName=""
      fi
   done
}

# Get the name of the item in the Trash.
getFileName PATH

function RestoreItem
{

# Restore the item.
mv "$APPDIR/$FileName" "PATH"

# See what is in the trash.
stuff=`ls /root/.Trash`

getFileName $APPDIR

if [ $FileName = $stuff ]
then

cp -f "TRASH/trashcan_empty.png" "TRASH/.DirIcon"

rox -x TRASH

fi

# Delete this file and directory.
rm -fr "$APPDIR"

}

# If they chose the "Restore" option.
if [ "$1" = "-restore" ]
then

# See if there is already a file or folder of the same name in the same location.
if [ -e PATH ]
then

MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 550x90 -title 	"Error" "An item named PATH already exists.
Do you want to overwite it?"

# If they chose to cancel.
[ $? -eq 21 ] || exit

fi

# Restore the item.
RestoreItem

else

# Check to see if they clicked on the application or dropped a file or directory on the icon.
test -sd "$@"
if [ "$?" = "0" ]
then

# If they just clicked on the icon.

# Wright the confirmation message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 550x90 -title 	"Confirm Action" "Do you want to restore the item PATH?"

# If they chose to cancel.
[ $? -eq 21 ] || exit

# See if there is already a file or folder of the same name in the same location.
if [ -e PATH ]
then

MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 550x90 -title 	"Error" "An item named PATH already exists.
Do you want to overwite it?"

# If they chose to cancel.
[ $? -eq 21 ] || exit

fi

# Restore the item.
RestoreItem

# If they chose the see-path option.
elif [ "$1" = "-see-path" ]
then

# Wright the message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Okay" -center -geometry 360x90 -title 	"Original File Location" "PATH"

else

# If they dropped something to the icon.

# Wright the error message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Okay" -center -geometry 360x90 -title 	"Error" "You can not drop items onto things that are in the Trash."

fi

fi
